/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "GestureViewWidget.h"
#include "MouseControl.h"
#include "GestureRecordDlg.h"

extern CoListItems g_mainOperations;
extern CoListItems g_dragDirections;

GestureViewWidget::GestureViewWidget(int gesture, CoListItem* operItem, QWidget* parent) : QWidget(parent)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_gesture = gesture;
    m_operItem = operItem;

    QHBoxLayout* mainlayout = new QHBoxLayout(this);
    setLayout(mainlayout);
    m_label = new ui::OnyxLabel(gestureString(), this);

    m_bRecord = new ui::OnyxPushButton(tr("记录"), this);
    m_bDelete = new ui::OnyxPushButton(tr("删除"), this);
    m_bOperation = new ui::OnyxPushButton(m_operItem->str, this);

    mainlayout->addWidget(m_label);
    mainlayout->addWidget(m_bRecord);
    mainlayout->addWidget(m_bDelete);
    mainlayout->addStretch(1);
    mainlayout->addWidget(m_bOperation);

    connect(m_bRecord, SIGNAL(clicked()), this, SLOT(recordClicked()));
    connect(m_bDelete, SIGNAL(clicked()), this, SLOT(deleteClicked()));
    connect(m_bOperation, SIGNAL(clicked()), this, SLOT(operationClicked()));

    Out_Log(__FUNCTION__, COCLASSNAME);
}

QString GestureViewWidget::gestureString()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    int dragEvent = m_gesture;
    QString str;

    if (dragEvent == 0)
    {

        Out_Log(__FUNCTION__, COCLASSNAME);
        return tr("无");
    }

    while (dragEvent)
    {
        int type = LAST_DRAG_ACT(dragEvent);
        if (type != 0)
        {
            str.prepend(g_dragDirections[type - 1].str);
        }
        DEL_DRAG_ACT(dragEvent);
    }

#ifndef QT_ARCH_ARM
    str += QString::number(m_gesture);
#endif

    Out_Log(__FUNCTION__, COCLASSNAME);
    return str;
}

void GestureViewWidget::recordClicked()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    int gesture = GestureRecordDlg::recordGesture(this->parentWidget()->parentWidget());
    if (gesture != -1)
    {
        m_gesture = gesture;
        m_label->setText(gestureString());
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void GestureViewWidget::deleteClicked()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    emit deleted(this);

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void GestureViewWidget::operationClicked()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    CoListItem* item = CoListDlg::select(&g_mainOperations);
    parentWidget()->parentWidget()->update();
    if (item != NULL)
    {
        m_operItem = item;
        m_bOperation->setText(item->str); 
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
}
